package com.masterclient.app.modules.aftersale.contract;

import android.widget.Toast;

import com.eoner.common.bean.base.CommonBaseBean;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselibrary.bean.aftersale.AfterSaleListBean;
import com.eoner.baselibrary.utils.Utils;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2017/08/26
*/
public class AfterSaleListPresenter extends AfterSaleListContract.Presenter{

    @Override
    public void getAfterSaleListBean(String page, String page_size) {
        Observable<AfterSaleListBean> observable = RetrofitUtil.getInstance().refundOrderList(page,page_size);
        Disposable disposable = toSubscribe(observable, new Consumer<AfterSaleListBean>() {
            @Override
            public void accept(@NonNull AfterSaleListBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.getAfterListSuccess(bean.getData());
                }else {
                    mView.getAfterListFailed(bean.getMsg());
                }
            }
        },new ThrowableConsumer());

        mRxManager.add(disposable);
    }

    @Override
    public void cancelAfterSale(String refund_id) {
        Observable<CommonBaseBean> observable = RetrofitUtil.getInstance().closeAfterSale(refund_id);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.closeAfterSaleSuccess();
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        },new ThrowableConsumer());

        mRxManager.add(disposable);
    }
}